﻿using System;
using System.Windows.Forms;
using DesignerHelper.EventHandlers;
using DesignerHelper.Main;
using SpreadsheetGear;
using WorkbookDesigner.Addons.SpreadsheetEditor.UI;
using WorkbookDesigner.Addons.WorkbookExplorer;

namespace WorkbookDesigner.Main
{
    public class MainWindowPresenter
    {
        public MainWindowPresenter(IMainWindow mainwindow)
        {
            Mw = mainwindow;

            Operator = new FileOperator(Mw);
        }
        private IMainWindow Mw { get; }
        public FileOperator Operator { get; private set; }



        public void Init(string[] args)
        {
            Operator.InitManager();
            if (args.Length > 0)
            {
                foreach (string location in args)
                {
                    Mw.FileOperator.OpenFile(location);
                }
            }
            else
                Mw.FileOperator.NewFile();

        }

        public void Init(IWorkbookSet wbs)
        {
            Operator.InitManager(wbs);
        }

        public void menu_Addons_MyWbExplorer_Click()
        {
            if (Mw.ActiveView != null)
                WbExplorer.GetInstance(Mw);
            else
                DesignerHelper.Help.MesBox.ErrorMessage("Error!");

        }

        public void menu_Addons_SpreadsheetEditor_Click()
        {
            try
            {
                using (EditorForm form = new EditorForm())
                {
                    form.ShowDialog();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show($"{exception.Message}\n{exception.TargetSite}\n{exception.StackTrace}");
            }

        }

        public void Menu_InsertDropDownOpening(ToolStripMenuItem pageBreak)
        {
            if (Mw.ActiveBookForm == null)
            {
                pageBreak.Enabled = false;
                return;
            }
            pageBreak.Text = Mw.ActiveBookForm.HasPageBreaks ? "移除当前工作表中的分隔符" : "插入页面分隔符";
            IRange r = Mw.ActiveView.RangeSelection;
            if (r.Row == 0 || r.Column == 0)
                pageBreak.Enabled = false;
            else
                pageBreak.Enabled = true;

        }


    }
}
